package cn.uexpo.lhz.controller.admin;


import cn.uexpo.lhz.biz.*;
import cn.uexpo.lhz.domain.ManageEquipment;
import cn.uexpo.lhz.domain.ManageEquipmentExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
 * spring boot 容器加载后自动监听
 * @author ge
 * @date
 */
@Component
public class MyRunner implements ApplicationRunner  {
	@Autowired
	private  ManageEquipmentBiz manageEquipmentBiz;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		//启动短信发送线程
		//        SendMessageRunner sendMessageRunner = new SendMessageRunner( eventBiz,equipmentBiz,sendMessageBiz,  manyBiz,  camerastatusBiz,  cameranotiyBiz,  msgBiz);
		//        Thread thread = new Thread(sendMessageRunner);
		//        thread.start();;

		while (true) {
//			System.out.println("2=========================================start");
			try {
				List<ManageEquipment> equipments = null;
				ManageEquipmentExample example=new ManageEquipmentExample();
				ManageEquipmentExample.Criteria criteria=example.createCriteria();
//				criteria.andExtstr2EqualTo("1");
				equipments=manageEquipmentBiz.selectByExample(example);

//		        equipments = manageEquipmentBiz.selectAll();
				for(ManageEquipment equipment:equipments) {
					String time = equipment.getExtstr3();
		               	if(null!=time&&!"".equals(time)) {
//		               		System.out.println(equipment.getDeviceid()+"==========time========================" + time);
			                  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			                  Date date = null;
			                  Date lDate = new Date();
								try {
									date = sdf.parse(time);
								} catch (ParseException e1) {
									e1.printStackTrace();
								}
			                    long mqDate = date.getTime();
			                    long lastDate = lDate.getTime();
								long diffDuration = lastDate-mqDate;

//								System.out.println(equipment.getName()+"================"+"diffDuration++++++++++++++++++++++++++++++++"+diffDuration);
								if (diffDuration > 60 * 1000L) {
									int num = 0;
									if(null!=equipment.getExtstr4()&&!"".equals(equipment.getExtstr4())) {
										num = Integer.parseInt(equipment.getExtstr4());
										num++;
										equipment.setExtstr4(String.valueOf(num));
									}else {
										equipment.setExtstr4("1");
										num = 1;
									}
									if(num>=3) {
										equipment.setState("0");
										equipment.setExtstr4("0");
									}
//									System.out.println("断开连接"+num+"次"+diffDuration);

			                    	 manageEquipmentBiz.updateByPrimaryKeySelective(equipment);
								}else {
									equipment.setExtstr4("0");
									equipment.setState("1");
			                    	manageEquipmentBiz.updateByPrimaryKeySelective(equipment);
								}
		               	}else {
//		               		System.out.println("断开连接");
		               		equipment.setState("0");
	                    	 manageEquipmentBiz.updateByPrimaryKeySelective(equipment);

		               	}

				}

				Thread.sleep(1000);
			} catch (InterruptedException e) {

				e.printStackTrace();
			}

		}

	}
}
